package com.rlm.parken.client.views;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.xml.client.DOMException;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

public class DatenParsenInArrayList {

	static class Kneipe {
		final int id;
		final String name;
		final String ort;
		final String bild;

		public Kneipe(int id, String name, String ort, String bild) {
			this.id = id;
			this.name = name;
			this.ort = ort;
			this.bild = bild;

		}
	}

	public interface Callback {
		void onDataAvailable(List<Kneipe> Kneipen);
	}

	public void dateiEinlesen(final Callback callback) {
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
				URL.encode("kneipen.xml"));

		try {
			builder.sendRequest(null, new RequestCallback() {
				@Override
				public void onError(Request request, Throwable exception) {

					// Couldn't connect to server (could be timeout, SOP
					// violation, etc.)
					Window.alert("Couldn't retrieve XML");
				}

				@Override
				public void onResponseReceived(Request request,
						Response response) {

					if (200 == response.getStatusCode()) {
						// Process the response in response.getText()
						List<Kneipe> kneipenListe = kneipenListeErstellen(response
								.getText());
						callback.onDataAvailable(kneipenListe);

					} else {

						// Handle the error. Can get the status text from
						// response.getStatusText()
						Window.alert("Error: " + response.getStatusText());
					}
				}
			});

		} catch (RequestException e) {

			// Couldn't connect to server
			Window.alert("Couldn't connect to server to retrieve the XML: \n");
		}
	}

	private List<Kneipe> kneipenListeErstellen(String kneipenliste) {

		List<Kneipe> Kneipen = new ArrayList<Kneipe>();

		try {
			// parse the XML document into a DOM
			Document kneipenDOM = XMLParser.parse(kneipenliste);
			NodeList kneipenNodes = kneipenDOM.getElementsByTagName("Kneipe");

			for (int i = 0; i < kneipenNodes.getLength(); i++) {
				// name auslesen und in Liste ablegen
				String name = kneipenDOM.getElementsByTagName("name").item(i)
						.getFirstChild().getNodeValue();

				String ort = kneipenDOM.getElementsByTagName("ort").item(i)
						.getFirstChild().getNodeValue();

				String bild = kneipenDOM.getElementsByTagName("bild").item(i)
						.getFirstChild().getNodeValue();

				Kneipen.add(new Kneipe(i, name, ort, bild));

			}

		} catch (DOMException e) {

			Window.alert("Could not parse XML document.");

		}

		return Kneipen;
	}

}
